﻿/// <reference path="../plugins/jquery.layout-1.2.0.js" />
/// <reference path="Event.js" />
/// <reference path="../jquery.jqGrid-4.4.4/js/jquery.jqGrid.src.js" />
/// <reference path="../jquery-1.9.1.js" />


var Subscriber = {
    controls: {
        subscriberGrid: $("#event-list")

    },
    SignalRURL: "Queue/Check",
    Connection: null,
    init: function () {
        var subscriberGrid = Subscriber.controls.subscriberGrid;

        var jqGridDummyData = [
            { title: 'ITR', room: 'Burgos', personincharge: 'Syene Acosta' },
            { title: "Director Appointment", room: 'Burgos', personincharge: 'Syene Acosta' },
            { title: "Manager 101", room: "PJ's Office", personincharge: 'Sir PJ' },
            { title: "Lunch", room: "Pantry", personincharge: 'Ate' },
            { title: 'X-Ray', room: 'Alonzo', personincharge: 'Dr. Frederick Magalang' },
            { title: 'Blood Chem', room: 'Rizal', personincharge: 'Dr. Jerome Corpuz' },
            { title: 'Physical Exam', room: 'Ponder Haven 1', personincharge: 'Dr. Jeffrey Edubas' }

        ]

        subscriberGrid.jqGrid({
            url: "/queue/GetEvents",
            datatype: "json",
            jsonReader: { repeatitems: false },
            height: 250,
            width: 360,
            colModel: [
                { name: 'Name', index: 'Name', label: 'Event Name' },
                { name: 'Room', index: 'Room', label: 'Room' },
                {
                    name: 'action', index: 'action', label: ' ', fixed: true, width: 70, sortable: false,
                    formatter: function (cellvalue, options, rowObjec) {
                        var obj = JSON.stringify(rowObjec);
                        return '<span class="fui-eye-24" style="font-size:35px;padding-left:15px;cursor:pointer" onclick="Subscriber.AddEvent(' + options.rowId + ');"></span>';
                    }
                },
                { name: 'PersonInCharge', index: 'PersonInCharge', label: '', hidden: true },
            ],
            loadComplete: function () {
                var grid = $("#list");
                var ids = grid.getDataIDs();

                for (var i = 0; i < ids.length; i++) {
                    grid.setRowData(ids[i], false, { height: 20 + i * 2 });
                }
                // grid.setGridHeight('auto');
            }
        });

        var body = document.body,
            html = document.documentElement;

        var height = Math.max(body.scrollHeight, body.offsetHeight,
                               html.clientHeight, html.scrollHeight, html.offsetHeight);

        subscriberGrid.jqGrid("setGridHeight", height - 415);
        $("#subs-event-main-container").css("height", height - 115);


        Subscriber.Connection = $.connection(Subscriber.SignalRURL);

        Subscriber.Connection.received(function (data) {
            Subscriber.DecodeMessage(data);
        });

        Subscriber.Connection.start();
        Subscriber.InitEvents();

    },
    AddEvent: function (d) {

        if ($("#div-event-container").find(".unliq-event").length < 4) {

            var rowData = Subscriber.controls.subscriberGrid.jqGrid('getRowData', d);
            d = rowData;

            var prefix = d.Name.replace(/ /g, '').toLowerCase();
            $("#div-event-container").append("<div id='div-" + prefix + "'></div>");
            $('#div-' + prefix).sQevent({
                title: d.Name,
                room: d.room,
                personincharge: d.PersonInCharge

            });
        }



    },

    InitEvents: function () {

        $(document).on("click", ".btn-info", function () {

            var eventName = $(this).attr('eventname');

            Subscriber.Connection = $.connection(Subscriber.SignalRURL);

            Subscriber.Connection.start().done(function () {
                var result = Subscriber.Connection.send("RESERVE_QUEUE:" + eventName);
            });
           

        });

    },
    DecodeMessage: function (data) {

        if (data && data.Type) {

            var model = data.Model;
            switch (data.Type) {

                case "UPDATE_NOWSERVING":
                    {
                        $("#" + data.Model + "nowserving-num").html(data.Message);
                        break;
                    }
                case "RESERVE_QUEUE":
                    {
                        alert("under construction");
                    }
            }
            return;
        }
    }

}

$(document).ready(function () {
    Subscriber.init();
})
